package wsepForum.test.acceptance.bridge;

import wsepForum.common.elements.Section;
import wsepForum.common.elements.Topic;
import wsepForum.common.elements.User;
import wsepForum.common.elements.User.UserType;
import wsepForum.domain.ForumDomain;
import wsepForum.domain.Session;
import wsepForum.domain.SessionManager;
import wsepForum.persistance.CompassSearchAdaptor;
import wsepForum.persistance.DataHandler;
import wsepForum.persistance.SQLConnector;

/**
 * An implementation of the interface which bridge the actions in it to our forum system
 * @author Oren Ben-Haim
 *
 */
public class ForumActionsRealBridge implements IForumActions {


	/**
	 * the forum backbone
	 */
	private ForumDomain forumDomain;
	/**
	 *  Data handler that will be directed to a testing file
	 */
	private DataHandler dataHandler;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public ForumActionsRealBridge() {
		try {
			this.forumDomain = new ForumDomain(null);

			// creating the handler itself
			this.dataHandler = new DataHandler(new SQLConnector("wseptests"), CompassSearchAdaptor.getInstance(true));
			this.dataHandler.initDB();
			forumDomain.setPersistent(this.dataHandler);
			SessionManager.getInstance().setPersistent(forumDomain.getPersistent());
			// admin user
			User firstUser = new User("user1", "user1", UserType.ADMIN);
			forumDomain.getPersistent().setUser(firstUser);
			// non admin user
			User secondUser = new User("user2", "user2", UserType.MEMBER);
			forumDomain.getPersistent().setUser(secondUser);
		} catch (Exception e) {
			System.out.println("Problem with real bridge implementation");
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.test.acceptance.bridge.IForumActions#actionTryToLoginWith(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean actionTryToLoginWith(String username, String password) {
		Session session = this.forumDomain.createNewSession();
		try {
			return session.login(username, password);
		} catch (Exception e) {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.test.acceptance.bridge.IForumActions#actionTryToCreateSection(java.lang.String, boolean)
	 */
	@Override
	public boolean actionTryToCreateSection(String sectionName, boolean isAdmin) {
		Session session = this.forumDomain.createNewSession();
		Section newSection;
		try {
			if (isAdmin) {
				session.login("user1", "user1");
			}
			else {
				session.login("user2", "user2");
			}
			newSection = session.getHandler().createSection(0, sectionName, null);
		} catch (Exception e) {
			return false;
		}
		// gets the created section
		try {
			newSection = session.getHandler().getSection(newSection.getSectionHeader(0).getElementID());
		} catch (Exception e) {
			return false;
		}

		//assert that the section is the right one
		return (newSection != null && 0 == newSection.getParentID() && sectionName.equals(newSection.getHeadLine()));

	}

	public boolean actionTryToCreateTopic(String topicName, String body, boolean isLoggedOn) {
		Session session = this.forumDomain.createNewSession();
		Topic newTopic;
		try {
			if (isLoggedOn) {
				session.login("user2", "user2");
			}
			newTopic = session.getHandler().createTopic(0, topicName, body);
		} catch (Exception e) {
			return false;
		}
		// gets the created topic
		try {
			newTopic = session.getHandler().getTopic(newTopic.getElementID());
		} catch (Exception e) {
			return false;
		}
		//assert that the section is the right one
		return (newTopic != null && 0 == newTopic.getParentID() && topicName.equals(newTopic.getHeadLine()));

	}

	/* (non-Javadoc)
	 * @see wsepForum.test.acceptance.bridge.IForumActions#close()
	 */
	@Override
	public void closeBridge() {
		try {
			this.forumDomain.getPersistent().clearDB();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean actionTryToDeleteMessage(String subject, boolean isAuthor, boolean isAdmin) {

		Session session = this.forumDomain.createNewSession();
		Topic newTopic;
		try {

			if (isAdmin | isAuthor) {
				session.login("user1", "user1");
				newTopic = session.getHandler().createTopic(0, subject, "body");
			} else {
				session.login("user1", "user1");
				newTopic = session.getHandler().createTopic(0, subject, "body");
				session.login("user2", "user2");
			}

		} catch (Exception e) {
			return false;
		}
		// delete the message
		try {
			session.getHandler().deleteMessage(newTopic.getElementID(), newTopic.getMessage(0).getElementID());
		} catch (Exception e) {
			return false;
		}
		//assert that the action succeded
		return true;
	}





	@Override
	public boolean actionTryToDeleteTopic(String subject, boolean isAuthor, boolean isAdmin) {

		Session session = this.forumDomain.createNewSession();
		Topic newTopic;
		try {

			if (isAdmin | isAuthor) {
				session.login("user1", "user1");
				newTopic = session.getHandler().createTopic(0, subject, "body");
			} else {
				session.login("user1", "user1");
				newTopic = session.getHandler().createTopic(0, subject, "body");
				session.login("user2", "user2");
			}

		} catch (Exception e) {
			return false;
		}
		// delete the message
		try {
			session.getHandler().deleteMessage(newTopic.getElementID(), newTopic.getMessage(0).getElementID());
		} catch (Exception e) {
			return false;
		}
		//assert that the action succeded
		return true;
	}

	@Override
	public boolean changePassword(String username, String oldPass, String newPass) {
		Session session = this.forumDomain.createNewSession();
		try {
			session.login(username, oldPass);
			boolean result = session.getHandler().changePassword(oldPass, newPass);
			return result;
		} catch (Exception e) {
			return false;
		}
	}


}
